package com.lgl.controller;

import com.lgl.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private ICodeService codeService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/create/{email}")
    public String generateCode(@PathVariable String email) {
        String code = codeService.generateCode(email);
        String url = "http://lgl-service-email/email/" + email + "/" + code;
        Boolean result = restTemplate.getForObject(url, Boolean.class);
        if (result) {
            return code;
        }
        return "";
    }

    @GetMapping("/validate/{email}/{code}")
    public int validateCode(@PathVariable String email, @PathVariable String code) {
        return codeService.validateCode(email, code);
    }
}
